<template>
  <div class="total-box">
    <div class="app-container">
      <HkSearchform
        :data="queryParams"
        :field-list="filterInfo.fieldList"
        :list-type-info="dictList"
        @handleSearch="handleQuery"
        @handleReset="resetQuery"
        @handleEvent="handleEvent"
      />

      <el-row :gutter="10">
        <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新建</el-button>
        <el-button icon="el-icon-delete" size="small" :disabled="!ids.length" @click="handleDelete">删除</el-button>
        <el-button icon="el-icon-download" size="small" @click="importTemplate">下载模板</el-button>
        <el-button size="small" @click="handleImport">导入</el-button>
        <el-button size="small" @click="handleExport">导出</el-button>
      </el-row>

      <hk-table
        ref="tableRef"
        :loading="loading"
        :table-data="tableData"
        :table-columns="tableList"
        :selection="true"
        :show-index="true"
        :total="total"
        :operate="true"
        :table-height="80"
        :query-params="queryParams"
        @handleSelectionChange="handleSelectionChange"
        @changePage="getDatalist"
      >
        <template #unitType="scope">
          <dict-label :options="dictList.unitTypeList" :value="scope.row.unitType" />
        </template>
        <template #networkProtection="scope">
          <dict-label :options="dictList.equipmentProtectionList" :value="scope.row.networkProtection" />
        </template>
        <template #accessType="scope">
          <dict-label :options="dictList.accessTypeList" :value="scope.row.accessType" />
        </template>
        <template #accessIpRange="scope">
          <el-popover placement="bottom-start" width="200" trigger="hover">
            <div class="popver">
              <p>开始IP：{{ scope.row.access.startIp ||'' }}</p>
              <p>结束IP：{{ scope.row.access.endIp ||'' }}</p>
              <p>掩码：{{ scope.row.access.mask ||'' }}</p>
              <p>容量：{{ scope.row.access.count ||'' }}</p>
            </div>
            <span slot="reference">{{ scope.row.accessIpRange }}</span>
          </el-popover>
        </template>
        <template #connectIpRange="scope">
          <el-popover placement="bottom-start" width="200" trigger="hover">
            <div class="popver">
              <p>开始IP：{{ scope.row.connect.startIp||'' }}</p>
              <p>结束IP：{{ scope.row.connect.endIp||'' }}</p>
              <p>掩码：{{ scope.row.connect.mask ||'' }}</p>
              <p>容量：{{ scope.row.connect.count||'' }}</p>
            </div>
            <span slot="reference">{{ scope.row.connectIpRange }}</span>
          </el-popover>
        </template>
        <template #operate="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </hk-table>
    </div>
    <!-- 新增和编辑的对话框 -->
    <edit-dialog
      ref="editDialogRef"
      :title="title"
      :dict-list="dictList"
      @handleSubmit="getDatalist"
    />
    <!-- 输出 -->
    <import-dialog ref="import_dia" @submit="getDatalist" />
  </div>
</template>

<script>
import HkTable from '@/components/Hk-Table/index.vue'
import EditDialog from './component/emitDialog.vue'
import HkSearchform from '@/components/HkSearchform/index.vue'
import DictLabel from '@/components/Common/dictLabel.vue'
import ImportDialog from './component/ImportDialog.vue'
import { pageList, deleteList, listByTypes } from '@/api/approval/externalAccess.js'

export default {
  components: {
    HkTable,
    EditDialog,
    HkSearchform,
    DictLabel,
    ImportDialog
  },
  data() {
    return {
      treeId: '',
      // 搜索项配置
      filterInfo: {
        fieldList: [
          { label: '关键字', type: 'input', field: 'keyword' },
          {
            label: '接入方式',
            type: 'select',
            field: 'type',
            list: 'accessTypeList'
          }
        ]
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,

      // 表格数据
      tableData: [],
      //  表格
      tableList: [
        { label: '接入单位名称', param: 'unitName' },
        { label: '单位类型', param: 'unitType', type: 'slot' },
        { label: '接入网络名称', param: 'networkName' },
        { label: '网络防护等级', param: 'networkProtection', type: 'slot' },
        { label: '接入方式', param: 'accessType', type: 'slot' },
        { label: '接入IP地址段', param: 'accessIpRange', type: 'slot' },
        { label: '互联IP地址段', param: 'connectIpRange', type: 'slot' },
        { label: '审批时间', param: 'approvalTime', sortable: true }
      ],
      // 弹出层标题
      title: '',

      // 查询参数
      queryParams: {
        size: 10,
        current: 1
      },
      // 列表规则
      dictList: {
        accessTypeList: [{
          label: '网络接入',
          value: '1'
        }, {
          label: '远程终端接入',
          value: '2'
        }],
        equipmentProtectionList: [],
        unitTypeList: []
      }
    }
  },

  mounted() {
    this.getDatalist()
    listByTypes('unitType,equipmentProtection,accessType').then(res => {
      for (const key in this.dictList) {
        const type = key.slice(0, key.length - 4)
        this.dictList[key] = res[type].map(item => {
          return { label: item.itemLabel, value: item.itemVale }
        })
      }
    })
  },
  methods: {
    /** 查询用户列表 */
    getDatalist(val) {
      this.loading = true
      this.queryParams = { ...this.queryParams, ...val, scrollToTop: true }

      pageList(this.queryParams).then(res => {
        this.total = res.total
        this.tableData = res.list
        this.loading = false
        this.queryParams.scrollToTop = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery(val) {
      this.queryParams.current = 1
      this.getDatalist()
    },
    // 重置表单
    resetQuery() {
      for (const key in this.queryParams) {
        this.queryParams[key] = undefined
      }
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getDatalist()
    },
    // 搜索框变化
    handleEvent(val) {
      const field = val.field
      const value = val.value
      this.queryParams = {
        ...this.queryParams,
        ...{
          [field]: value
        }
      }
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.title = '添加'
      this.$refs.editDialogRef.show()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = '编辑'

      this.$refs.editDialogRef.show(row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids.join(',')
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          return deleteList(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getDatalist()
        })
        .catch(error => {
          console.log(error)
        })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.import_dia.upload.open = true
    },
    /** 下载模板操作 */
    importTemplate() {
      this.gDownload(window.baseUrl.icp + '/approve/access/template', {}, `外部接入权限审批模板.xlsx`)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.gDownload(
        window.baseUrl.icp + '/approve/access/export',
        {
          ...this.queryParams
        },
        `外部接入权限审批${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
  <style lang="scss" scoped>
  .app-container{
    display: flex;
    flex-direction: column;
  }
.total-box {
  font-size: 14px;
  display: flex;
  width: 100%;
  height: 100%;
}
.head-container {
  overflow: auto;
  padding: 20px;
  height: 100%;
  width: 270px;
  border-right: 1px solid rgba(0, 0, 0, 0.08);
}
.targetTable {
    flex: 1;
    overflow: hidden;
    padding-bottom: 0;
  }
.el-row {
  display: flex;
  flex-wrap: wrap;
  margin: 0 !important;
}
.popver{
  p+p{
    margin-top: 5px;
  }
}
::v-deep {
  .targetTable .el-table{
    overflow: hidden;
  }
}
</style>

